package com.wolfalpha.utron.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormal3Activity;
import com.wolfalpha.utron.service.PartnerService;
import com.wolfalpha.utron.service.impl.PartnerServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.EditAlertDialog;
import com.wolfalpha.utron.view.ListAlertDialog;
import com.wolfalpha.utron.view.RoleAlertDialog;

/**
 * 升级页面
 * <p/>
 * Created by luodon on 15/9/2.
 */
public class PartnerToGoodActivity extends BaseNormal3Activity implements View.OnClickListener {

    private TextView tvRole;        //角色
    private TextView tvAbility;     //技能
    private TextView tvWilling;     //意愿
    private TextView tvTimein;      //时间投入
    private TextView tvExper;       //工作经验
    private TextView tvDy;          //代言

    private PartnerService partnerService;

    private boolean isUpdate = false;       //是否为更新

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("update")) {
            finish();
        }
        isUpdate = bundle.getBoolean("update");

        setContentView(R.layout.activity_partner_edit_to_well);
        partnerService = new PartnerServiceImpl();

        initView();
        initListeners();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        setTvTitle("优秀合伙人");

        tvRole = (TextView) findViewById(R.id.tv_partner_edit_well_role);
        tvAbility = (TextView) findViewById(R.id.tv_partner_edit_well_ability);
        tvWilling = (TextView) findViewById(R.id.tv_partner_edit_well_willing);
        tvTimein = (TextView) findViewById(R.id.tv_partner_edit_well_timein);
        tvExper = (TextView) findViewById(R.id.tv_partner_edit_well_experience);
        tvDy = (TextView) findViewById(R.id.tv_partner_edit_well_dy);

        pdUpdate = new ProgressDialog(this);
        pdUpdate.setMessage("正在更新...");
        pdUpdate.setCancelable(false);


    }


    /**
     * 初始化监听
     */
    private void initListeners() {
        tvRole.setOnClickListener(this);
        tvAbility.setOnClickListener(this);
        tvWilling.setOnClickListener(this);
        tvTimein.setOnClickListener(this);
        tvExper.setOnClickListener(this);
        tvDy.setOnClickListener(this);
    }

    private final int UPDATE_SUCCESS = 1;
    private final int UPDATE_FAIL = 2;

    private ProgressDialog pdUpdate;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case UPDATE_SUCCESS:
                    pdUpdate.dismiss();
                    UIHelper.ToastMessage(PartnerToGoodActivity.this, "更新成功");
                    finish();
                    break;
                case UPDATE_FAIL:
                    pdUpdate.dismiss();
                    UIHelper.ToastMessage(PartnerToGoodActivity.this, "更新失败,请重试");
                    break;
            }
        }
    };

    @Override
    public void rightOperation() {

        if (TextUtils.isEmpty(role) || TextUtils.isEmpty(ablitity) || TextUtils.isEmpty(willing) || TextUtils.isEmpty(timeIn) || TextUtils.isEmpty(experience) || TextUtils.isEmpty(dy)) {
            UIHelper.ToastMessage(PartnerToGoodActivity.this, "数据不完整，请先完善信息");
            return;
        }
        pdUpdate.show();
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                boolean result = false;
                if (!isUpdate) {
                    result = partnerService.uptoGoodPartner(role, ablitity, willing, timeIn, experience, dy);
                } else {
                    result = partnerService.updatePartner(role, ablitity, willing, timeIn, experience, dy);
                }

                if (result) {
                    handler.sendEmptyMessage(UPDATE_SUCCESS);
                } else {
                    handler.sendEmptyMessage(UPDATE_FAIL);
                }
            }
        });

    }

    private String role = "";
    private String ablitity = "";
    private String willing = "";
    private String timeIn = "";
    private String experience = "";
    private String dy = "";

    /**
     * 选召唤类型
     */
    private void showRoles() {
        final RoleAlertDialog ad = new RoleAlertDialog(PartnerToGoodActivity.this);
        ad.setTitle("请选择期望的角色");
        ad.setOnFinishedListener(new RoleAlertDialog.OnFinishedListener() {
            @Override
            public void finish(String result) {
                tvRole.setText(result);
                role = result;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_partner_edit_well_role:
                showRoles();
                break;
            case R.id.tv_partner_edit_well_ability:
                showAbilityEdit();
                break;
            case R.id.tv_partner_edit_well_willing:
                showWillings();
                break;
            case R.id.tv_partner_edit_well_timein:
                showTimes();
                break;
            case R.id.tv_partner_edit_well_experience:
                showExpEdit();
                break;
            case R.id.tv_partner_edit_well_dy:
                showDyEdit();
                break;
        }
    }

    /**
     * 输入代言
     */
    private void showDyEdit() {
        final EditAlertDialog ad = new EditAlertDialog(PartnerToGoodActivity.this);
        ad.setHint("一句话介绍自己(优点哦~)");
        ad.setMostWords(30);
        ad.setOnFinishedListener(new EditAlertDialog.OnFinishedListener() {
            @Override
            public void finish(String input) {
                tvDy.setText(input);
                dy = input;
            }
        });
    }

    /**
     * 输入工作经验
     */
    private void showExpEdit() {
        final EditAlertDialog ad = new EditAlertDialog(PartnerToGoodActivity.this);
        ad.setHint("输入您的相关工作经验");
        ad.setMostWords(80);
        ad.setOnFinishedListener(new EditAlertDialog.OnFinishedListener() {
            @Override
            public void finish(String input) {
                tvExper.setText(input);
                experience = input;
            }
        });
    }

    /**
     * 选择时间投入
     */
    private void showTimes() {
        final ListAlertDialog ad = new ListAlertDialog(PartnerToGoodActivity.this);
        ad.setTitle("选择可投入的时间");
        final String[] arr = this.getResources().getStringArray(R.array.partner_times);
        ad.setArray(arr);
        ad.setOnFinishedListener(new ListAlertDialog.OnFinishedListener() {
            @Override
            public void finish(int input) {
                tvTimein.setText(arr[input]);
                timeIn = arr[input];
            }
        });
    }

    /**
     * 输入技能
     */
    private void showAbilityEdit() {
        final EditAlertDialog ad = new EditAlertDialog(PartnerToGoodActivity.this);
        ad.setHint("输入您的技能短语,逗号隔开");
        ad.setMostWords(15);
        ad.setOnFinishedListener(new EditAlertDialog.OnFinishedListener() {
            @Override
            public void finish(String input) {
                tvAbility.setText(input);
                ablitity = input;
            }
        });
    }

    /**
     * 选择意愿
     */
    private void showWillings() {
        final ListAlertDialog ad = new ListAlertDialog(PartnerToGoodActivity.this);
        ad.setTitle("选择创业意愿");
        final String[] arr = this.getResources().getStringArray(R.array.partner_willings);
        ad.setArray(arr);
        ad.setOnFinishedListener(new ListAlertDialog.OnFinishedListener() {
            @Override
            public void finish(int input) {
                tvWilling.setText(arr[input]);
                willing = arr[input];
            }
        });
    }
}
